package com.bocai.service.impl;

import com.bocai.bean.*;
import com.bocai.mapper.*;
import com.bocai.service.ICashInfoService;
import com.bocai.service.IDistributorCashInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;


/**
 * Created by jyf on 18/4/13.
 */
@Service
public class DistributorCashInfoServiceImpl implements IDistributorCashInfoService {

    @Autowired
    private TSalesOutMoneyInfoMapper tSalesOutMoneyInfoMapper;

    @Autowired
    private TSalesUserMapper tSalesUserMapper;

//    @Autowired
//    private MessageInfoMapper messageInfoMapper;

    @Override
    public int insertSelective(TSalesOutMoneyInfo tSalesOutMoneyInfo) {
        TSalesUser tSalesUser = tSalesUserMapper.selectByPrimaryKey(tSalesOutMoneyInfo.getDistributorId());
        if (null == tSalesUser) {
            return 0;
        } else {
            if (tSalesUser.getSalesStatus() == 0) {
                tSalesOutMoneyInfo.setCashAccount(tSalesUser.getSalesPhone());
                if (tSalesUser.getSalesMoney().compareTo(tSalesOutMoneyInfo.getCashMoney()) >= 0) {
                    tSalesUser.setSalesMoney(tSalesOutMoneyInfo.getCashMoney());//即将减入的钱
                    tSalesUserMapper.updateDistributorMoney(tSalesUser); //修改用户余额
                    return tSalesOutMoneyInfoMapper.insertSelective(tSalesOutMoneyInfo);
                } else {
                    return 2;
                }
            } else {
                return 3;
            }
        }
    }

//    @Override
//    public List<TSalesOutMoneyInfo> findAllCashInfo() {
//        return cashInfoMapper.selectAllCashInfos();
//    }
//
//    @Override
//    public int updateCashInfo(Integer putForwardId, Integer flag) {
//        CashInfo cashInfo = cashInfoMapper.selectByPrimaryKey(putForwardId);
//        if (null != cashInfo) {
//            if (cashInfo.getCashState() == 1) {
//                TUserInfo tUserInfo2 = userInfoMapper.selectByPrimaryKey(cashInfo.getUserId());
//                if (null == tUserInfo2) {
//                    return 0;
//                } else {
//                    if (tUserInfo2.getUserStatus() == 1) {
//                        HashMap<String, Object> map = new HashMap<>();
//                        map.put("cashId", cashId);
//                        map.put("flag", flag);
//                        map.put("createTime", new Date());
//                        int num = cashInfoMapper.updateCashInfo(map);
//                        if (num > 0) {
//                            MessageInfo messageInfo = new MessageInfo();
//                            messageInfo.setUserId(tUserInfo2.getUserId());
//                            messageInfo.setCreateTime(new Date());
//                            if (flag == 1) {
//                                messageInfo.setContent("The money is already on the account. Please pay attention to it.");
//                            } else {
//                                messageInfo.setContent("If you have refused to present it, please check the balance of the account.");
//                            }
//                            messageInfo.setTitle("Present information");
//                            HashMap<String, Object> map2 = new HashMap<>();
//                            map2.put("userId", messageInfo.getUserId());
//                            userInfoMapper.updateReadState(map2);
//                            messageInfoMapper.insertSelective(messageInfo);
//                            switch (flag) {
//                                case 1:
//                                    // 确定 钱不做任何改变
//                                    return 1;
//                                case 2:
//                                    // 驳回 钱原路返回
//                                    TUserInfo tUserInfo = new TUserInfo();
//                                    tUserInfo.setUserId(cashInfo.getUserId());
//                                    tUserInfo.setUserAccount(cashInfo.getCashMoney());
//                                    return userInfoMapper.updateAddCashUserMoney(tUserInfo);
//                                default:
//                                    return 0;
//                            }
//                        } else {
//                            return 0;
//                        }
//                    } else {
//                        return 2;
//                    }
//                }
//            } else {
//                return 0;
//            }
//
//        } else {
//            return 0;
//        }
//    }
}
